// pages/home-page/train/train.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customerId: '',
    customerSex: 'female',
    productId: '',
    types: [
      {
        name: '产品讲解',
        code: 'proposal',
        icon: 'icon-jihuashu',
        hidden: true,
        list: [],
      }, {
        name: '异议处理',
        code: 'objection',
        icon: 'icon-yiyi',
        hidden: true,
        list: [],
      }, {
        name: '约访训练',
        code: 'visit',
        icon: 'icon-yuefang',
        hidden: true,
        list: [],
      }, {
        name: '寒暄破冰',
        code: 'greeting',
        icon: 'icon-hanxuan',
        hidden: true,
        list: [],
      },
    ],
    CDNPATH: app.globalData.CDNPATH,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { customerId, productId } = options
    this.setData({
      customerId,
      productId,
    })
    this.addStage()
    this.getCustomer()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addStage () {
    const { customerId, productId } = this.data
    app.api.post({
      url: 'customer_stage_add',
      data: {
        customerId,
        productId,
        stage: 'practice' // 训练阶段
      }
    })
  },
  getCustomer () {
    app.api.post({
      url: 'customer_manage_detail',
      data: {
        id: this.data.customerId
      }
    }).then(res => {
      this.setData({
        customerSex: res.sex === '男' ? 'male' : 'female'
      })
    })
  },
  toggle (e) {
    const { types, productId } = this.data
    const { index, type } = e.currentTarget.dataset
    const { hidden, list, code } = types[index]
    if (list.length) {
      this.setData({
        [`types[${index}].hidden`]: !hidden
      })
    } else {
      this.getSceneList(productId, code).then(res => {
        this.setData({
          [`types[${index}].hidden`]: !hidden,
          [`types[${index}].list`]: res
        })
      })
    }
  },
  getSceneList (productId, sceneBelong) {
    console.log(productId, sceneBelong)
    return new Promise((resolve, reject) => {
      app.api.post({
        url: 'product_scenes',
        data: {
          productId,
          sceneBelong
        }
      }).then(res => {
        if (sceneBelong === 'proposal') {
          res = res.map(item => {
            item.productId = productId
            return item
          })
        }
        resolve(res)
      })
    })
  },
  goToDemonstration () {
    const { customerId, productId } = this.data
    wx.navigateTo({
      url: `/pages/home-page/demonstration/demonstration?customerId=${customerId}&productId=${productId}`
    })
  }
})